package com.wwg.market.ui.category.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.mobileares.midp.mui.client.table.UFishTable;
import com.mobileares.midp.widgets.client.button.ButtonBar;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.panel.ContentPanel;
import com.mobileares.midp.widgets.client.pop.IMessageConfirm;
import com.mobileares.midp.widgets.client.pop.Message;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.wwfish.gwt.model.client.IFishModel;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.ListModel;
import com.wwfish.gwt.model.client.impl.Listener;
import com.wwfish.gwt.model.client.impl.ListenerHandler;
import com.wwg.market.ui.category.client.widgets.CategoryPropertyForm;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.dashboard.client.utils.CacheCode;
import com.wwg.market.ui.dashboard.client.widgets.PageItem;
import com.wwg.market.ui.service.dto.AttributeDto;
import com.wwg.market.ui.service.dto.CategoryDto;
import com.wwg.market.ui.service.interfaces.ProductAttributeService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-5
 * Time: 18:35:05
 * To change this template use File | Settings | File Templates.
 */
public class CategoryPropertiesItem extends PageItem<CategoryDto> {
    private UFishTable<AttributeDto> table;
    private CategoryDto category;
    private CategoryPropertyForm form;
    private CButton modify;
    private CButton delete;
    private ContentPanel tableWrapper;


    @Override
    protected void processMain(SimplePanel mainPanel) {
        ViewTable tu = new ViewTable(getColumn());
        tu.addSelectionHandler(new SelectionHandler<TableItem>() {
            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
                modify.setEnabled(true);
                delete.setEnabled(true);
            }
        });
        table = new UFishTable(tu);
        form = new CategoryPropertyForm();

        tableWrapper = new ContentPanel();
        tableWrapper.setContent(table);
        mainPanel.setWidget(tableWrapper);
    }

    @Override
    protected void processMenu(ButtonBar bar) {
        CButton back = new CButton("返回");
        CButton add = new CButton("添加");
        modify = new CButton("修改");
        delete = new CButton("删除");
        bar.addButton(back);
        bar.addButton(add);
        bar.addButton(modify);
        bar.addButton(delete);
        back.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                getPage().switchItem(CategoryOperPageItem.class.getName());
            }
        });

        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                AttributeDto dto = new AttributeDto();
                dto.setCategoryId(category.getId());
                form.show(dto);
            }
        });
        modify.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                form.show((AttributeDto) table.getBMUI().getSelectedItem().getUserObject());
            }
        });
        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                Message.showConfirm("温馨提示", "是否要删除当前属性?", new IMessageConfirm() {
                    public void fire(boolean confirm) {
                        if (confirm) {
                            final AttributeDto dto = (AttributeDto) table.getBMUI().getSelectedItem().getUserObject();
                            Utils.invoke(ProductAttributeService.class.getName(), "delete", new Object[]{dto}, new LoadingAsyncCallback() {
                                @Override
                                protected void success(Object o) {
                                    table.getFishModel().deleteItem(dto);
                                }
                            });
                        }
                    }
                });

            }
        });

    }

    @Override
    public void initModel() {
        table.setFishModel((ListModel<AttributeDto>) FishModelManager.createTempModel(FishModelManager.TYPE_LIST));
        table.getFishModel().getListenerManager().addListener(Listener._LOAD, new ListenerHandler() {
            public void execute(IFishModel model) {
                modify.setEnabled(false);
                delete.setEnabled(false);
            }
        });
         form.setListModel(table.getFishModel());
    }

    @Override
    public void ddOutModel(CategoryDto ob) {
        category = ob;
        Utils.invoke(ProductAttributeService.class.getName(), "getAttributeByCategory", new Object[]{category.getId()}, new LoadingAsyncCallback() {
            @Override
            protected void success(Object o) {
                table.getFishModel().load((List) o);
                tableWrapper.setHead("[" + category.getName() + "]属性列表");
            }
        });
    }

    private ColumnWaiter getColumn() {
        ColumnExt col1 = new ColumnExt("属性名称", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                AttributeDto dto = (AttributeDto) cellItem.getUserObject();
                cellItem.setText(dto.getName());
            }
        });
        ColumnExt col2 = new ColumnExt("类型", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                AttributeDto dto = (AttributeDto) cellItem.getUserObject();
                cellItem.setText(CacheCode.getNameByCode(CacheCode.getCodesById(MenuImpl.categoryPropertyType),dto.getType()));
            }
        });
        ColumnExt col3 = new ColumnExt("是否必填", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                AttributeDto dto = (AttributeDto) cellItem.getUserObject();
                HTML html = new HTML();
                if (dto.getRequired())
                    html.setHTML("<font color='red'><b>是</b></font>");
                else
                    html.setHTML("<font color='green'><b>否</b></font>");

                cellItem.setWidget(html);
            }
        });
        ColumnWaiter cw = new ColumnWaiter(new ColumnExt[]{col1, col2, col3});
        return cw;
    }
}
